
const tools = require('../util/tool')
module.exports = class admin_sql extends require('./mysql'){
    static getUserByTypeCharMod(type,inputText,CharType,currPage,pageNum){
        let currTime = tools.formatDate()
        pageNum = Number(pageNum);
        currPage = Number(currPage);
        currPage = Number(currPage * pageNum)
        return new Promise((resolve, reject) => {
            let sql = 'select * from `user` where '+ CharType+' like \ "%'+inputText+'%" \ and type = '+type+' order by createtime desc LIMIT ?,?'
            console.log(sql)
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }

    static getUsersByTypeCharTotal(type,inputText,CharType){
        return new Promise((resolve, reject) => {
            let sql = 'select count(1) as count from user where '+ CharType+' like "%'+inputText+'%" and type = '+type
            console.log(sql)
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }

    /**
     * 总表查询
     * @param username
     * @param type
     * @param startDate
     * @param endDate
     * @param currPage
     * @param pageNum
     * @returns {Promise<any>}
     */
    static getUserDataMod(username,type,startDate,endDate, currPage,pageNum){


        pageNum = Number(pageNum)
        currPage = Number(currPage)
        currPage = Number(pageNum * currPage)
        return new Promise((resolve, reject)=>{
            // let sql="select * from `user` where username like '%"+username+"%' and type="+type+" limit ?,?"
            let sql="select * from `user` where 1=1"
            if (username) sql += " and username like '%"+username+"%'"
            if (type) sql += " and type= '" + type + "'"
            if (startDate && endDate) sql += " and (createtime BETWEEN '"+startDate+"' AND '"+endDate+"')"
            sql += " limit ?,?";

            console.log(sql)
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                console.log(this.formarParams(currPage,pageNum))
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }

    /**
     * 模糊查询分页
     * @param username
     * @param type
     * @param startDate
     * @param endDate
     * @returns {Promise<any>}
     */
    static getUserDataTotal(username,type,startDate,endDate){
        return new Promise((resolve, reject)=>{
            // let sql="select count(1) as count from `user` where username like '%"+username+"%' and type="+type+" "
            let sql="select count(1) as count from `user` where 1=1"
            if (username) sql += " and username like '%"+username+"%'"
            if (type) sql += " and type= '" + type + "'"
            if (startDate && endDate) sql += " and (createtime BETWEEN '"+startDate+"' AND '"+endDate+"')"
            // if (!startDate && !endDate) sql = sql + " and (createtime >= ? and createtime <= ?)"
            console.log(sql)
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }



    static getMenuDataSql(prentId,currPage,pageNum){
        pageNum = Number(pageNum)
        currPage = Number(currPage)
        currPage = Number(pageNum * currPage)
        return new Promise((resolve, reject)=>{
            // let sql="select * from `user` where username like '%"+username+"%' and type="+type+" limit ?,?"
            let sql="select * from `menu` where prentId = "+ prentId+" limit ?,?"
            console.log(sql)
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }

    static getMenuDataTotal(prentId){
        return new Promise((resolve, reject) => {
            let sql = 'select count(1) as count from `menu` where prentId = '+prentId;
            console.log(sql)
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


}